home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 36 / load.zip / NEWLOAD.C < prev    next >
Text File  |  1988-10-15  |  5KB  |  189 lines

  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <api.h>
  4. #include "spawn.hdr"
  5.  
  6. unsigned int _STKSIZ={8192/16};
  7. unsigned int _HEAPSIZ={8192/16};
  8. unsigned int _STKLOW={1}; /* force stack below heap */
  9.               /* this forces minimum memory usage */
  10.  
  11. char dv_loc[200] = {0,0,0,0,0,0,0,0,0,0,
  12.     "This is the leader for the structure"};
  13. struct _dv_loc { /* actual structure format for dv_loc */
  14.     char header[10]; /* leader of 10 zero bytes */
  15.     char identifier[50]; /* holds the leader string for locating */
  16.     char dv_path[139]; /* holds the directory path */
  17.     char cksum; /* holds the correcting checksum */
  18. } *dv;
  19.  
  20. /* current directory/path */
  21. char cur_path[139];
  22.  
  23. main(argc,argv)
  24. int argc;
  25. char **argv;
  26. {
  27.     char work[100];
  28.     /* activate the DESQVIEW system if present */
  29.     if(!api_dvver()) {
  30.         fprintf(stderr,"Load: Desqview must be loaded\n");
  31.         exit(255);
  32.     }
  33.     api_level(0x0200); /* must have API level 2.x */
  34.  
  35.     /* get current directory and path */
  36.     cur_path[0] = (bdos(0x19,0,0) + 'A');
  37.     cur_path[1] = ':';
  38.     cur_path[2] = 0;
  39.     getcwd(work,99);
  40.     if(*work != '/' && *work != '\\')
  41.         strcat(cur_path,"\\");
  42.     strcat(cur_path,work);
  43.  
  44.     /* test for initialization call */
  45.     dv = (struct _dv_loc *) dv_loc;
  46.     if(!*(dv->dv_path)) /* need initialization */
  47.         run_setup();
  48.  
  49.     /* test for legal argument counts and call process loader */
  50.     if(argc < 2) {
  51.         fprintf(stderr,"usage: load [&]ii [parameters]\n");
  52.         exit(255);
  53.     }
  54.  
  55.     /* spawn the process */
  56.     spawn(argc,argv);
  57.     exit(0);
  58.  
  59. }
  60.  
  61. /*
  62. *run the setup program for this file
  63. *read newload.exe copying it to load.exe
  64. *until the string in dv_loc is found
  65. *then add the current directory string into the new file
  66. *and copy the rest of the file to load.exe
  67. */
  68. run_setup()
  69. {
  70.     FILE *fin, *fout;
  71.     register int ch, mode;
  72.     char *q;
  73.     fprintf(stderr,"newload: creating load.exe\n");
  74.     if((fin = fopen("newload.exe","r")) == NULL) {
  75.         fprintf(stderr,"newload: cannot access newload.exe - aborting\n");
  76.         exit(255);
  77.     }
  78.     if((fout = fopen("load.exe","w")) == NULL) {
  79.         fprintf(stderr,"newload: cannot create load.exe - aborting\n");
  80.         exit(255);
  81.     }
  82. srchloop:
  83.     mode = 0;
  84.     while((ch = getc(fin)) != EOF) {
  85.         putc(ch,fout);
  86.         if(ch != dv->identifier[mode])
  87.             goto srchloop;
  88.         ++mode;
  89.         if(!ch)
  90.             goto change; /* got the string */
  91.     }
  92.     fprintf(stderr,"newload: invalid newload.exe - no identifier string\n");
  93.     exit(255);
  94. change:
  95.     /* got id string, skip path array */
  96.     while(mode < 50) {
  97.         ch = getc(fin);
  98.         putc(ch,fout);
  99.         ++mode;
  100.     }
  101.     for(mode = 0; mode < 140; ++mode)
  102.         getc(fin);
  103.     /* then write out current path + checksum byte */
  104.     ch = 0;
  105.     for(mode = 0; mode < 139; ++mode) {
  106.         putc(cur_path[mode],fout);
  107.         ch = (ch ^ cur_path[mode]);
  108.     }
  109.     putc(ch,fout);
  110.     /* then copy rest of file over */
  111.     while((ch = getc(fin)) != EOF)
  112.         putc(ch,fout);
  113.     fclose(fin);
  114.     fclose(fout);
  115.     fprintf(stderr,"newload: load.exe successfully created\n");
  116.     exit(0);
  117. }
  118.  
  119. /*
  120. *spawn function for spawning new processes
  121. */
  122. int spawn(argc,argv)
  123. int argc;
  124. char **argv;
  125. {
  126.     register char *p;
  127.     register int i;
  128.     int force_foreground;
  129.     int status;
  130.     struct _spawn_blk t;
  131.     long temp, app_start();
  132.  
  133.     /* if invoked with &, force to foreground after spawning */
  134.     if(force_foreground = (*argv[1] == '&'))
  135.         ++argv[1];
  136.  
  137.     /* get the appropriate .dvp file for this job */
  138.     if(strlen(argv[1]) != 2) {
  139.         fprintf(stderr,"load: pif label (%s) must be 2 characters\n",argv[1]);
  140.         exit(255);
  141.     }
  142.     i = strlen(dv->dv_path) - 1;
  143.     if(dv->dv_path[i] != '/' && dv->dv_path[i] != '\\')
  144.         strcat(dv->dv_path,"\\");
  145.     strcat(dv->dv_path,argv[1]);
  146.     strcat(dv->dv_path,"-pif.dvp");
  147.     if((i = open(dv->dv_path,O_RDONLY)) < 0 || read(i,&t,416) != 416) {
  148.         if(i >= 0)
  149.             close(i);
  150.         fprintf(stderr,"load: unable to locate file %s to spawn process\n",
  151.             dv->dv_path);
  152.         exit(255);
  153.     }
  154.     close(i);
  155.  
  156.     /* set up the process file for spawning */
  157.     /* set null drive/directory to current */
  158.     if(t.defdrive == ' ' && *t.defdirectory == 0) {
  159.         t.defdrive = cur_path[0];
  160.         strcpy(t.defdirectory, &cur_path[2]);
  161.     }
  162.  
  163.     /* if in command field and */
  164.     /* there are items left in command line, use them */
  165.     if(argc > 2  && strcmp(t.progparms,"?") == 0) {
  166.         t.progparms[0] = 0;
  167.         for(i = 2; i < argc; ++i) {
  168.             strcat(t.progparms," ");
  169.             strcat(t.progparms,argv[i]);
  170.         }
  171.         if(strlen(t.progparms) > 63) {
  172.             fprintf(stderr,"load: command line too long\n");
  173.             exit(255);
  174.         }
  175.     }
  176.  
  177.     /* spawn the new process now */
  178.     temp = app_start(&t,416);
  179.  
  180.     if(!temp) {
  181.         fprintf(stderr,"load: load %s failed\n", argv[1]);
  182.         exit(255);
  183.     }
  184.  
  185.     if(force_foreground)
  186.         app_gofore(tsk_me());
  187.  
  188. }
  189.